package com.heima.wemedia.controller.v1;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.*;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.common.vo.WmNewsCommentVo;
import com.heima.wemedia.pojos.CommentLikeDto;
import com.heima.wemedia.pojos.CommentListVo;
import com.heima.wemedia.service.WmCommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/api/v1/comment/manage")
public class WmCommentController {

    @Autowired
    private WmCommentService wmCommentService;

    //查看评论
    @PostMapping("/find_news_comments")
    public PageResponseResult<List> findNewsComments(@RequestBody CommentPageDto dto) {
        log.info("接收到参数：{}", dto);
        Page<WmNewsCommentVo> newsComments = wmCommentService.findNewsComments(dto);
        return PageResponseResult.okResult(
                (int) newsComments.getCurrent(),
                (int) newsComments.getSize(),
                (int) newsComments.getTotal(),
                newsComments.getRecords()
        );
    }

    //删除评论
    @DeleteMapping("/del_comment/{commentId}")
    public ResponseResult deleteNewsComments(@PathVariable("commentId") Integer commentId) {
        return wmCommentService.deleteNewsComments(commentId);
    }

    //回复评论
    @PostMapping("/comment_repay")
    public ResponseResult repayComment(@RequestBody CommentDto commentDto) {
        return wmCommentService.repayComment(commentDto);
    }

    //打开或关闭评论
    @PostMapping("/update_comment_status")
    public ResponseResult updateCommentStatus(@RequestBody CommentDto commentDto) {
        return wmCommentService.updateCommentStatus(commentDto);
    }


    /**
     * 删除评论回复
     *
     * @param commentRepayId
     * @return
     */
    @DeleteMapping("/del_comment_repay/{commentRepayId}")
    public ResponseResult delCommentRepay(@PathVariable String commentRepayId) {
        log.info("接收到参数id：{}", commentRepayId);
        wmCommentService.delCommentRepay(commentRepayId);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @PostMapping("/list")
    public PageResponseResult<List> list(@RequestBody CommentPageDto2 dto) {
        log.info("接收到参数：{}", dto);
        Page<CommentListVo> listComment = wmCommentService.listComment(dto);
        return PageResponseResult.okResult(
                (int) listComment.getCurrent(), (int) listComment.getSize(),
                (int) listComment.getTotal(), listComment.getRecords()
        );
    }


    /**
     * 文章评论点赞
     * @param params
     * @return
     */
    @PostMapping("/like")
    public ResponseResult commentlike(@RequestBody Map<String,String> params){
        log.info("文章评论点赞接口，入参={}",params);
        String commentId = params.get("commentId");
        String operation = params.get("operation");
        if (operation == null || commentId == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        return wmCommentService.commentlike(commentId,Integer.valueOf(operation));
    }



}
